<?php

namespace  app\command\controller;

use common\model\sell66\UserOrder;
use common\model\sell66\BotInv;
use common\bot\SteamBotService;
use common\service\InvService;
use think\Exception;

/**
 * 检查存入报价状态
 * 
 * @author zhouqiang
 * @date 2018-05-29
 */
class CheckFromTrade
{

	protected $userOrderId = 0;

	protected function getUserOrderByCheckAll($userOrderId = NULL)
	{
		return UserOrder::getUserOrderByCheckAll($userOrderId);
	}

	protected function rpcCheckFrom($fromTradeId)
	{
		return SteamBotService::getInstance()->rpcCheckFrom($fromTradeId);
	}

	protected function updateUserOrder($userOrderId, $botId, $tradeofferId, $status, $details)
	{
		return UserOrder::updateUserOrder($userOrderId, $botId, $tradeofferId, $status, $details);
	}

	protected function getUserOrderByOrderId($userOrderId)
	{
		return UserOrder::getUserOrderByOrderId($userOrderId);
	}

	protected function insertBotINV($invs)
	{
		return BotInv::insertBotINV($invs);
	}

	protected function checkTradeAll()
	{
		$orders = $this->getUserOrderByCheckAll();
		if (!$orders) {
			echo 'query waiting check for accept failed'."\r\n";
			return false;
		}

		foreach ($orders as $order) {
			if ($order['from_trade_id'] <= 0) {
				continue;
			}
			$this->checkTrade($order['user_order_id'], $order);
		}
		
		return true;
	}

	protected function checkTrade($userOrderId, array $order)
	{
		$trade = $this->rpcCheckFrom($order['from_trade_id']);
		if (!$trade['code']) {
			echo 'query trade status failed, from_trade_id:'.$order['from_trade_id']."\r\n";
			return false;
		}

		$data = $trade['data'];

		$botId = $data['bot_id'];
		$tradeofferId = $data['tradeoffer_id'];
		$status = $data['tradeoffer_status'];
		$details = isset($data['details']) ? $data['details'] : [];

		if ($order['tradeoffer_status'] == $status) {
			echo 'no change in the trade status, user_order_id: '.$userOrderId."\r\n";
			return false;
		}

		$res = $this->updateUserOrder($userOrderId, $botId, $tradeofferId, $status, $details);
		if (!$res) {
			echo 'update user order failed, user_order_id: '.$userOrderId."\r\n";
			return false;
		}

		if ($status == UserOrder::$tradeOfferStatusMap['success']) {
			$res = $this->BotINV($userOrderId);
			if (!$res) {
				echo 'insert bot inv failed, user_order_id: '.$userOrderId."\r\n";
				return false;
			}
            InvService::getInstance()->resetUserInventoryStatus($order['steam_id'], $order['app_id']);
			echo 'insert bot inv success'."\r\n";
		}

		echo 'update user order success'."\r\n";
		return true;
	}

	protected function BotINV($userOrderId)
	{
		$order = $this->getUserOrderByOrderId($userOrderId);

		$data = array();
		foreach ($order['orderdetails'] as $details) {
			$details = array(
				'user_order_id' => $order['user_order_id'],
				'user_id' => $order['user_id'],
				'app_id' => $order['app_id'],
				'bot_id' => $order['bot_id'],
				'o_id' => $details['new_assetid'],
				'class_id' => $details['class_id'],
				'instance_id' => $details['instance_id'],
				'market_name' => $details['market_name'],
				'market_hash_name' => $details['market_hash_name'],
				'asset_hash' => '',
				'cd_time' => $order['app_id'] == 730 ? time() + 3600 * 24 * 8: 0,
				'action_link' => $details['action_link'],
				'rarity_color' => $details['rarity_color'],
			);
			$data[] = $details;
		}

		return $this->insertBotINV($data);
	}

	public function handle($userOrderId = NULL)
	{
		$this->userOrderId = $userOrderId;

		$this->checkTradeAll();
		echo '数据查询完毕！'."\r\n";
		return true;
	}
}